package com.example.demo.controller.usercontroller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo.common.Result;
import com.example.demo.context.UserContext;
import com.example.demo.dto.CommentDTO;
import com.example.demo.entity.Comment;
import com.example.demo.service.CommentService;
import com.example.demo.vo.CommentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user/community/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 获取评论列表
     */
    @GetMapping
    public Result<Map<String, Object>> getCommentList(
            @RequestParam Long postId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        IPage<CommentVO> page = commentService.getCommentList(postId, pageNum, pageSize);
        Map<String, Object> data = new HashMap<>();
        data.put("total", page.getTotal());
        data.put("pages", page.getPages());
        data.put("current", page.getCurrent());
        data.put("size", page.getSize());
        data.put("records", page.getRecords());
        return Result.success(data);
    }

    /**
     * 发表评论
     */
    @PostMapping
    public Result<Long> createComment(@RequestBody CommentDTO commentDTO) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO, comment);
        return Result.success(commentService.createComment(comment));
    }

    /**
     * 回复评论
     */
    @PostMapping("/reply")
    public Result<Long> replyComment(@RequestBody CommentDTO commentDTO) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO, comment);
        return Result.success(commentService.replyComment(comment));
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteComment(@PathVariable Long id) {
        return Result.success(commentService.deleteComment(id, UserContext.getUserId()));
    }

    /**
     * 点赞评论
     */
    @PostMapping("/{id}/like")
    public Result<Boolean> likeComment(@PathVariable Long id) {
        return Result.success(commentService.likeComment(id, UserContext.getUserId()));
    }

    /**
     * 取消点赞评论
     */
    @PostMapping("/{id}/unlike")
    public Result<Boolean> unlikeComment(@PathVariable Long id) {
        return Result.success(commentService.unlikeComment(id, UserContext.getUserId()));
    }
} 